package com.sxz.sbs.module.web;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.base.BasePresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 基类webView
 */
public class BaseWebViewActivity extends BaseActivity {


    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.pb)
    ProgressBar pb;
    @BindView(R.id.web_view)
    WebView webView;
    private WebViewConfig webViewConfig;
    @Override
    protected void init() {
        webViewConfig=new WebViewConfig(this,webView,pb);
        _init();
        if (!TextUtils.isEmpty(getRootUrl())) {
            if (getPostData() == null) {
                webViewConfig.loadUrl(getRootUrl());
            } else {
                webViewConfig.loadUrl(getRootUrl(), getPostData());
            }
        } else {
            ToastUtils.showShort("url不允许为空!");
            this.finish();
        }
    }
    protected void _init() {

    }

    /**
     * 获取url携带的数据
     * @return
     */
    protected  byte[] getPostData(){
        return null;
    }

    /**
     * 获取url
     * @return
     */
    protected  String getRootUrl(){
        return null;
    }
    public void setToolbarTitle(String titleName){
        webTitle.setText(titleName);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_web_view;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @OnClick({R.id.web_back, R.id.web_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.web_back:
                webViewConfig.onBackPress();
                break;
            case R.id.web_close:
                finish();
                break;
            default:
                break;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        webViewConfig.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        webViewConfig.onPause();
    }

    @Override
    protected void onDestroy() {
        webViewConfig.onDestroy();
        super.onDestroy();

    }

    @Override
    public void onBackPressedSupport() {
        super.onBackPressedSupport();
        webViewConfig.onBackPress();
    }

}
